package io.renren.modules.vastFinance.service.impl;

import com.alibaba.excel.EasyExcel;
import io.renren.modules.vastFinance.listener.ExcelFinanceListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.vastFinance.dao.FinanceDao;
import io.renren.modules.vastFinance.entity.FinanceEntity;
import io.renren.modules.vastFinance.service.FinanceService;


@Service("financeService")
public class FinanceServiceImpl extends ServiceImpl<FinanceDao, FinanceEntity> implements FinanceService {
        @Autowired
        FinanceDao financeDao;



    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<FinanceEntity> page = this.page(
                new Query<FinanceEntity>().getPage(params),
                new QueryWrapper<FinanceEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<FinanceEntity> getAllListByPid(Integer parentId) {

        QueryWrapper<FinanceEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",parentId);
        List<FinanceEntity> list = financeDao.selectList(queryWrapper);
        for (FinanceEntity financeEntity : list) {
            Boolean hasChildren = this.hasChildren(financeEntity.getFinId());
            financeEntity.setHasChildren(hasChildren);
        }
        return list;
    }
    private Boolean hasChildren(Integer parentId){
       QueryWrapper<FinanceEntity> queryWrapper = new QueryWrapper<>();
       queryWrapper.eq("parent_id",parentId);
       Integer count = financeDao.selectCount(queryWrapper);
       if(count.intValue()>0){
           return true;
       }
       return false;
   }

    /**
     * 导入数据（读数据）
     * @param inputStream
     */
    @Override
    public void importData(InputStream inputStream) {
        EasyExcel.read(inputStream,FinanceEntity.class, new ExcelFinanceListener()).sheet().doRead();
    }

    /**
     * 下载财务表
     * @return
     */
    @Override
    public List<FinanceEntity> financeData() {
          //查询数据库数据
        List<FinanceEntity> financeList = financeDao.selectList(null);
        return  financeList;
    }

}